////////////////////////////////////////////////////////////////////////////////
//
//  ADOBE SYSTEMS INCORPORATED
//  Copyright 2005-2007 Adobe Systems Incorporated
//  All Rights Reserved.
//
//  NOTICE: Adobe permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////

package mx.logging
{

	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;

	import mx.resources.IResourceManager;
	import mx.resources.ResourceManager;

	/**
	 *  The logger that is used within the logging framework.
	 *  This class dispatches events for each message logged using the <code>log()</code> method.
	 */
	public class LogLogger extends EventDispatcher implements ILogger, IEventDispatcher
	{

		private var _enabled:Boolean;

		/**
		 *  The separator string to use between fields (the default is " ")
		 */
		public var fieldSeparator:String=" ";

		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------

		/**
		 *  Constructor.
			 *
			 *  @param category The category for which this log sends messages.
		 */
		public function LogLogger(category:String)
		{
			super();

			_category=category;
		}


		public function get enabled():Boolean
		{
			return _enabled;
		}

		public function set enabled(value:Boolean):void
		{
			_enabled=value;
		}

		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------

		/**
		 *  @private
		 *  Used for accessing localized Error messages.
		 */
		private var resourceManager:IResourceManager=ResourceManager.getInstance();

		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------

		//----------------------------------
		//  category
		//----------------------------------

		/**
		 *  @private
		 *  Storage for the category property.
		 */
		private var _category:String;

		/**
		 *  The category this logger send messages for.
		 */
		public function get category():String
		{
			return _category;
		}

		/** 支持string或class */
		public function getLogger(categorys:Object):ILogger
		{
			if (categorys == null)
			{
				return null;
			}
			var key:String=null;
			if (categorys is String)
			{
				key=_category + "." + categorys;
			}
			else
			{
				key=_category + "." + getQualifiedClassName(categorys).replace(/:+/img, ".");
			}
			if (key)
			{
				return Log.getLogger(key);
			}
			return null;
		}


		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------

		private function cheackEnaled():Boolean
		{
			if (Log.enabled || _enabled)
			{
				//不能使用
				return true;
			}
			return false;
		}

		/**
		 *  @inheritDoc
		 */
		public function log(level:int, ... message):void
		{
			if (cheackEnaled())
			{
				return;
			}
			// we don't want to allow people to log messages at the 
			// Log.Level.ALL level, so throw a RTE if they do
			if (level < LogEventLevel.DEBUG)
			{
				var msg:String=resourceManager.getString("logging", "levelLimit");
				throw new ArgumentError(msg);
			}

			if (hasEventListener(LogEvent.LOG))
			{
				dispatchEvent(new LogEvent(message.join(fieldSeparator), level));
			}
		}

		/**
		 *  @inheritDoc
		 */
		public function debug(... message):void
		{
			if (cheackEnaled())
			{
				return;
			}
			if (hasEventListener(LogEvent.LOG))
			{
				dispatchEvent(new LogEvent(message.join(fieldSeparator), LogEventLevel.DEBUG));
			}
		}

		/**
		 *  @inheritDoc
		 */
		public function error(... message):void
		{
			if (cheackEnaled())
			{
				return;
			}
			if (hasEventListener(LogEvent.LOG))
			{
				dispatchEvent(new LogEvent(message.join(fieldSeparator), LogEventLevel.ERROR));
			}
		}

		/**
		 *  @inheritDoc
		 */
		public function fatal(... message):void
		{
			if (cheackEnaled())
			{
				return;
			}
			if (hasEventListener(LogEvent.LOG))
			{
				dispatchEvent(new LogEvent(message.join(fieldSeparator), LogEventLevel.FATAL));
			}
		}

		/**
		 *  @inheritDoc
		 */
		public function info(... message):void
		{
			if (cheackEnaled())
			{
				return;
			}
			if (hasEventListener(LogEvent.LOG))
			{
				dispatchEvent(new LogEvent(message.join(fieldSeparator), LogEventLevel.INFO));
			}
		}

		/**
		 *  @inheritDoc
		 */
		public function warn(... message):void
		{
			if (cheackEnaled())
			{
				return;
			}
			if (hasEventListener(LogEvent.LOG))
			{
				dispatchEvent(new LogEvent(message.join(fieldSeparator), LogEventLevel.WARN));
			}
		}
	}

}
